package geowiki.dataThread;


import processing.opengl.*;

import java.util.List;

import geowiki.data.Link;
import geowiki.data.Place;
import geowiki.data.PlacemakerPlaceExtractor;
import geowiki.data.YQLLinkExtractor;
import geowiki.data.YQLQuery;
import geowiki.utils.WikipediaUtils;
import geowiki.vis.LineFromPlaces;
import geowiki.vis.PlaceMarker;
import java.util.ArrayList;
import processing.core.PApplet;
import processing.core.PImage;

public class PlacesExtractionThreadTestApp extends PApplet {

    List<Link> links;
    LineFromPlaces lines = new LineFromPlaces(this);
    PImage worldMap;
    List<Place> mainPagePlaces;
    List<PlaceMarker> mainPlaceMarkers;

    int offset = 200;

    public void setup() {
        size(1200, 650+offset);
        smooth();

        worldMap = loadImage("worldmap-equirectangular-blank-dark2.png");


   // String article = "Blues";
    String article = "Che_Guevara";

        links = loadLinks(article);

        List<Place> mainPagePlaces = loadPlacesViaPlacemakerFromWiki(article);

        mainPlaceMarkers = createPlaceMarkers(mainPagePlaces);

        println("Found " + links.size() + " links.");

        for (int i = 0; i < links.size(); i++) {
            PlacesExtractionThread pet = new PlacesExtractionThread(this, links.get(i));
            pet.start();
        }






    }

    public void draw() {
        background(0);

       

        image(worldMap, 0, offset, width, height-offset);


        int color = color(255, 180, 0);
        int colorline = color(255, 180, 0);

        if (links != null) {
            for (int i = 0; i < links.size(); i++) {
                Link link = links.get(i);



                lines.drawJumper(link.storedPlaceMarkers, colorline, 3,1.1f);



                for (int j = 0; j < link.storedPlaceMarkers.size(); j++) {

                    link.storedPlaceMarkers.get(j).displayVeryVerySmall(color);

                    //println("drawing Link# " + i + " Place# " + j);
                }
            }



        }

        color = color(255);
        colorline = color(255);

        if (mainPlaceMarkers != null) {
            for (int k = 0; k < mainPlaceMarkers.size(); k++) {
                mainPlaceMarkers.get(k).displayVerySmall(color);
                
            }
        } else {
            println("NOMAINPLACES");
        }

       
       // lines.drawJumper(mainPlaceMarkers, colorline, 20,3);
        lines.drawJumper(mainPlaceMarkers, colorline, 50,1.1f);
    }

    public List<Link> loadLinks(String wikipediaArticleName) {

        YQLLinkExtractor linkExtractor = new YQLLinkExtractor(this);
        String wikipediaURL = WikipediaUtils.getWikipediaArticleURL(wikipediaArticleName);
        println(wikipediaURL);
        String restURL = YQLQuery.getAllLinksFromWebpage(wikipediaURL);
        List<Link> alllinks = linkExtractor.getLinks(restURL);
        return alllinks;

    }

    public List<Place> loadPlacesViaPlacemakerFromWiki(String wikipediaArticleName) {

        String wikipediaURL = WikipediaUtils.getWikipediaArticleURL(wikipediaArticleName);
        List<Place> allplaces = loadPlacesViaPlacemakerFromURL(wikipediaURL);

        return allplaces;
    }

    public List<Place> loadPlacesViaPlacemakerFromURL(String URL) {


        List<Place> allPlaces = new ArrayList<Place>();

        YQLLinkExtractor linkExtractor = new YQLLinkExtractor(this);
        String[] paragraphs = linkExtractor.getMainParagraphs(URL);

        PlacemakerPlaceExtractor placeExtractor = new PlacemakerPlaceExtractor(this);
        for (int i = 0; i < paragraphs.length; i++) {
            PApplet.println("Getting places for " + i + ". p");
            List<Place> places = placeExtractor.getPlaces(paragraphs[i]);

            PApplet.println("\tplaces=" + places);
            allPlaces.addAll(places);
        }
        return allPlaces;
    }

    List<PlaceMarker> createPlaceMarkers(List<Place> places) {
        List<PlaceMarker> placeMarkers = new ArrayList<PlaceMarker>();
        for (int i = 0; i < places.size(); i++) {
            Place place = places.get(i);
            PlaceMarker placeMarker = new PlaceMarker(this, place,offset);
            placeMarkers.add(placeMarker);
        }
        return placeMarkers;
    }
}
